package com.gt.login;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.res.AssetManager;
import android.util.Log;

public class FileUtils {
	public static void mkDir(String file) {
		File f = new File(file);
		if (!f.exists()) {
			f.mkdirs();
		}
	}

	public static void saveStringToFile(String string, String fileName) {
		// write to file
		FileWriter out;
		try {
			out = new FileWriter(fileName);
			out.write(string);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	private static void copyFile(InputStream in, OutputStream out)
			throws IOException {
		byte[] buffer = new byte[1024];
		int read;
		while ((read = in.read(buffer)) != -1) {
			out.write(buffer, 0, read);
		}
		in.close();
		in = null;
		out.flush();
		out.close();
		out = null;
	}

	public static void CopyAssets(AssetManager assetMgr) {
		AssetManager assetManager = assetMgr;
		String[] files = null;
		try {
			files = assetManager.list("");
		} catch (IOException e) {
			Log.e("tag", e.getMessage());
		}
		for (String filename : files) {
			InputStream in = null;
			OutputStream out = null;
			try {
				in = assetManager.open(filename);
				out = new FileOutputStream(Constants.IP_USER_XML_DATA_PATH
						+ filename);
				copyFile(in, out);
			} catch (Exception e) {
				Log.e("tag", e.getMessage());
			}
		}
	}
}
